package com.j2eefast.produce.pms.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.common.core.utils.ToolUtil;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.sys.entity.SysUserEntity;
import com.j2eefast.framework.sys.service.SysUserService;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.Global;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.produce.pms.entity.*;
import com.j2eefast.produce.pms.service.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>样品检测数据录入</p>
 *
 * @author: summer
 * @date: 2020-04-27 22:03
 * @web: http://www.j2eefast.com
 * @version: 1.0.1
 */
@Controller
@RequestMapping("/pms/entry")
public class PmsSampleEntryController extends BaseController {
	private String urlPrefix = "modules/pms/entry";
	@Autowired
	private PmsSampleEntryService pmsSampleEntryService;
//	@Autowired
//	private PmsRegisterEntrustService pmsRegisterEntrustService;
//	@Autowired
//	private PmsEntrustService pmsEntrustService;

	@Autowired
	private PmsCementDataService pmsCementDataService;

	@Autowired
	private PmsEntrustService pmsEntrustService;
	@Autowired
	private PmsChargeStandardListService pmsChargeStandardListService;


	@RequiresPermissions("pms:entry:view")
	@GetMapping()
	public String process() {
		return urlPrefix + "/index";
	}

	@RequestMapping("/list")
	@RequiresPermissions("pms:entry:list")
	@ResponseBody
	public ResponseData list(@RequestParam Map<String, Object> params) {
		PageUtil page = pmsSampleEntryService.findPage(params);
		return success(page);
	}

	/**
	 * 删除
	 */
	@BussinessLog(title = "删除", businessType = BusinessType.DELETE)
	@RequestMapping( value = "/del", method = RequestMethod.POST)
	@RequiresPermissions("pms:entry:del")
	@RequiresRoles(Constant.SU_ADMIN)
	@ResponseBody
	public ResponseData delete(Long[] ids) {
		return pmsSampleEntryService.deleteByIds(ids)?success(): error("删除失败!");
	}


	/**
	 * 修改出入库 页面
	 */
	@RequiresPermissions("pms:entry:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap) {
		PmsSampleEntryEntity pmsEntrustEntity = pmsSampleEntryService.getById(id);
		mmap.put("entryEntity", pmsEntrustEntity);
		return urlPrefix + "/edit";
	}

	/**
	 * 修改出入库保存
	 entrust_code
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:entry:edit")
	@BussinessLog(title = "修改", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody

	public ResponseData edit(PmsSampleEntryEntity pmsEntrustEntity) {
		ValidatorUtil.validateEntity(pmsEntrustEntity);
		return pmsSampleEntryService.updateById(pmsEntrustEntity) ? success() : error("修改失败!");
	}

	/**
	 * 新增参数配置
	 */
	@RequiresPermissions("pms:entry:add")
	@GetMapping("/add")
	public String add() {
//		return urlPrefix + "/yuanshishuju2";
//		return urlPrefix + "/entering";
		return urlPrefix + "/yuanshijilu4";
	}


	/**
	 * 新增保存
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:entry:add")
	@BussinessLog(title = "认领", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData addSave(@Validated PmsSampleEntryEntity pmsEntrustEntity) {
		//校验参数
		ValidatorUtil.validateEntity(pmsEntrustEntity);
		return pmsSampleEntryService.saves(pmsEntrustEntity) ? success() : error("新增失败!");
	}



	/**
	 * 数据录入
	 */
	@GetMapping("/dataEntry/{id}")
	public String raw(@PathVariable("id") Long id, ModelMap mmap){
		//mmap.put("comp",  busClassifyService.findCompById(id));

		PmsSampleEntryEntity sampleEntry=pmsSampleEntryService.getById(id);
		PmsEntrustEntity pmsEntrustEntity=pmsEntrustService.selectEntrust(sampleEntry.getEntrustCode());
		String[] arr = pmsEntrustEntity.getInspectionItems().split(","); // 用,分割
		System.out.println(Arrays.toString(arr));
		List<PmsChargeStandardListEntity> pmsChargeStandardListEntity=pmsChargeStandardListService.selectChargeStandardList(arr);
		for(int i=0;i<pmsChargeStandardListEntity.size();i++){
			System.out.println(pmsChargeStandardListEntity.get(i));
		}

		mmap.put("dataEntry",  sampleEntry);
		mmap.put("chargeStandard",pmsChargeStandardListEntity);
		return urlPrefix + "/raw";
	}

	/**
	 * 数据录入保存
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:entry:dataEntry")
	@BussinessLog(title = "数据录入保存", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/dataEntry", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData CementAdd(@Validated PmsCementDataEntity pmsCementDataEntity) {
		//校验参数
		ValidatorUtil.validateEntity(pmsCementDataEntity);
		return pmsCementDataService.saves(pmsCementDataEntity) ? success() : error("数据录入失败!");
	}

	/**
	 * 修改数据录入
	 */
	@RequiresPermissions("pms:entry:editData")
	@GetMapping("/editData/{id}")
	public String editData(@PathVariable("id") Long id, ModelMap mmap) {
		PmsCementDataEntity pmsCementDataEntity = pmsCementDataService.getByIds(id);
        System.out.println("22222");
		System.out.println(pmsCementDataEntity.getXidu());
		System.out.println("22222");
		mmap.put("cementEntity", pmsCementDataEntity);
		return urlPrefix + "/editRaw";
	}

	/**
	 * 修改数据录入保存
	 entrust_code
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:entry:editData")
	@BussinessLog(title = "修改", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/editData", method = RequestMethod.POST)
	@ResponseBody

	public ResponseData editData(PmsSampleEntryEntity pmsEntrustEntity) {
		ValidatorUtil.validateEntity(pmsEntrustEntity);
		return pmsSampleEntryService.updateById(pmsEntrustEntity) ? success() : error("修改失败!");
	}



//	@BussinessLog(title = "检测录入", businessType = BusinessType.EXPORT)
//	@RequiresPermissions("pms:output:export")
//	@PostMapping("/export")
//	@ResponseBody
//	public ResponseData export(@RequestParam Map<String, Object> params) throws Exception {
//		String fileName = "检测录入";
//		fileName = ToolUtil.encodingExcelFilename(fileName);
//		String folder = Global.getTempPath() + File.separator + "pio" + File.separator;
//		List<PmsSampleEntryEntity> listData = pmsEntrustService.findList(params);
////内容样式策略
//		WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
////垂直居中,水平居中
//		contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//		contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//		contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
//		contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
//		contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
//		contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
////设置 自动换行
//		contentWriteCellStyle.setWrapped(true);
//// 字体策略
//		WriteFont contentWriteFont = new WriteFont();
//// 字体大小
//		contentWriteFont.setFontHeightInPoints((short) 12);
//		contentWriteCellStyle.setWriteFont(contentWriteFont);
////头策略使用默认
//		//excel如需下载到本地,只需要将response.getOutputStream()换成File即可(注释掉以上response代码)
//		EasyExcel.write(folder + fileName, PmsSampleEntryEntity.class)
//				//设置输出excel版本,不设置默认为xlsx
//				.excelType(ExcelTypeEnum.XLS).head(PmsSampleEntryEntity.class)
//				//设置拦截器或自定义样式
////				.registerWriteHandler(new MonthSheetWriteHandler())
////				.registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle))
//				.sheet("检测录入")
//				//设置默认样式及写入头信息开始的行数
//				.useDefaultStyle(true).relativeHeadRowIndex(3)
//				//这里的addsumColomn方法是个添加合计的方法,可删除
//				.doWrite(listData);
//		return success(fileName);
//	}




}
